package com.lanyuan.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {
	public static Date strToDate(String str){
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
	    try {
			return sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}  
	    return null;
	}
	public static String dateToString(Date date){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(date);
	}
	public static Date getFirstDay(){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		 //获取当前月第一天：
        Calendar c = Calendar.getInstance();   
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        String first = format.format(c.getTime());
        return strToDate(first);
	}
	public static Date getLastDay(){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		//获取当前月最后一天
        Calendar ca = Calendar.getInstance();   
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH)); 
        String last = format.format(ca.getTime());
        return strToDate(last);
	}
}
